from django.contrib import admin

from post.models import Topic, Comment


# Register your models here.

@admin.register(Topic)                   # 注册 模型和模型扩展类 方法二(两种方法二选一)
class TopicAdmin(admin.ModelAdmin):

    # 定制列表页面(基础) 开始
    # 列表中增加操作员显示
    # 练习1 TODO
    list_display = ['id', 'title', 'created_time', 'last_modified', 'is_online']

    # 自定义列(例如: 增加邮箱一列)
    # def email(self, obj):
    #     return obj.user.email
    # email.short_description = '邮箱'

    # list_per_page = 6  # 每页显示数

    # list_max_show_all = 20  # "显示全部"数量上限,缺省200

    # 排序规则
    # ordering = ['-last_modified']

    # 列表显示链接列
    # list_display_links = ['title']

    # 过滤器
    # list_filter = ['is_online']

    # 搜索栏
    # search_fields = ['^title', '=user__username']
    # search_fields = ['title__startswith', 'user__username__iexact']

    # 定制列表页面(基础) 结束

    # 定制表单页面(基础) 开始
    # fields = [('title', 'is_online'), 'user', 'content']

    # fieldsets = (
    #     ("基本信息", {
    #         'fields': [('title', 'is_online'), 'user']
    #     }),
    #     ('其它信息', {
    #         # 分组折叠，组合使用
    #         'classes': ('collapse',),
    #         'fields': ['content'],
    #     })
    # )

    # 练习3 TODO
    # fields = [('title', 'is_online'), 'user', 'content', 'content_length']
    # readonly_fields = ['content_length']
    #
    # def content_length(self, obj):
    #     return TODO
    # content_length.short_description = '话题内容长度'

    # raw_id_fields = ['user']

    # 定制表单页面(基础) 结束

    # 其它定制(进阶) 开始

    # 只显示标题包含'大数据'的话题
    # def get_queryset(self, request):
    #     return self.model._default_manager. \
    #         filter(title__contains="大数据")

    # 只显示当天的话题 TODO
    # def get_queryset(self, request):
    #     today = TODO
    #     return self.model._default_manager. \
    #         filter(TODO)

    # 练习4 TODO
    # def get_queryset(self, request):
    #     author = TODO
    #     return self.model._default_manager. \
    #         filter(TODO)

    # def has_delete_permission(self, request, obj=None):
    #     # 禁用删除按钮
    #     return False

    # 批量下线
    # actions = ["batch_offline"]
    # def batch_offline(self, request, queryset):
    #     rows = queryset.update(is_online=False)
    #     self.message_user(request, "%s行数据下线" % rows)
    # batch_offline.short_description = "批量下线"

    # 练习5 TODO
    # 批量上线
    # actions = TODO
    # def batch_online(self, request, queryset):
    #     rows = queryset.update(TODO)
    #     self.message_user(request, "%s行数据TODO" % rows)
    # batch_online.short_description = "批量TODO"

    # 利用save_model定制实例对象的保存操作
    # def save_model(self, request, obj, form, change):
    #     print("保存数据前进行一些操作")
    #     super().save_model(request, obj, form, change)
    #     print("保存数据后进行一些操作")

    # 其它定制(进阶) 结束

# admin.site.register(Topic, TopicAdmin) # 注册 模型和模型扩展类 方法一(两种方法二选一)


# 练习2 TODO
# @admin.register(Comment)
# class CommentAdmin(admin.ModelAdmin):

    # list_display = ['content', 'created_time', 'user']

    # list_filter = ['user']

    # search_fields = ['content', 'topic__title__startswith', 'user__username__iexact']

    # list_per_page = 6 # 每页显示数
